package com.love.utils;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper {
	private static final String DB_NAME = "alarms.db";
	private static final int VERSION = 1;
	private SQLiteDatabase db;
	private SQLiteHelper sqlHelper;
	private Context context;
	
	public DBHelper(Context context){
		this.context = context;
		sqlHelper = new SQLiteHelper(context, DB_NAME, null, VERSION);
		db = sqlHelper.getWritableDatabase();
	}
	
	public void close(){
		db.close();
		sqlHelper.close();
	}
	
	/**
	 * 
	 * @param alarm
	 * @return _id generated by database if success, otherwise -1
	 */
	public int insert(AlarmInfo alarm){
		String name = alarm.description;
		long time = alarm.timeinmillis;
		int frequency = alarm.frequency;
		int daysofweek = alarm.daysofweek;
		boolean enable = alarm.enable;
		ContentValues values = new ContentValues();
		values.put(AlarmInfo.DESCRIPTION, name);
		values.put(AlarmInfo.TIME, time);
		values.put(AlarmInfo.FREQUENCY, frequency);
		values.put(AlarmInfo.DAYS_OF_WEEK, daysofweek);
		values.put(AlarmInfo.ENABLE, enable + "");
		
		long rowid = db.insert(SQLiteHelper.DB_NAME, null, values);
		if(rowid == -1)
			return -1;
				
		Cursor cursor = db.rawQuery("select last_insert_rowid() from "+SQLiteHelper.DB_NAME,null);          
   
		if(cursor.moveToFirst())  
		   return cursor.getInt(0);  
		else
			return -1;
	}
	
	public AlarmInfo query(int _id){
		String name;
		long time;
		int frequency;
		boolean enable;
		int daysofweek;
		Cursor cursor = db.query(SQLiteHelper.DB_NAME, null, "_id="+_id, null, null, null, null);
		if(cursor.getCount() != 1){
			return null;
		}else{
			cursor.moveToFirst();
			name = cursor.getString(1);
			time = cursor.getLong(2);
			frequency = cursor.getInt(3);
			daysofweek = cursor.getInt(4);
			enable = Boolean.parseBoolean(cursor.getString(5));
			return new AlarmInfo(_id, name, time, frequency, enable, daysofweek);
		}
	}
	
	public ArrayList<AlarmInfo> getAlarmList(){
		ArrayList<AlarmInfo> alarmlist = new ArrayList<AlarmInfo>();
		Cursor cursor = db.query(SQLiteHelper.DB_NAME, null, null, null, null, null, AlarmInfo.ID + " " + "ASC");
		cursor.moveToFirst();
		while(!cursor.isAfterLast()){
			int _id = cursor.getInt(0);
			String name = cursor.getString(1);
			long time = cursor.getLong(2);
			int frequency = cursor.getInt(3);
			String enablestring = cursor.getString(5);
			boolean enable = Boolean.parseBoolean(cursor.getString(5));
			int daysofweek = cursor.getInt(4);
			AlarmInfo alarm = new AlarmInfo(_id, name, time, frequency, enable, daysofweek);
			alarmlist.add(alarm);
			cursor.moveToNext();
		}
		cursor.close();
		return alarmlist;
	}
	
	public AlarmInfo update(AlarmInfo alarm){
		AlarmInfo ret;
		if(alarm.get_id() == -1){
			int id = insert(alarm);
			alarm.set_id(id);
			ret = alarm;
		}else{
			onlyupdate(alarm);
			ret = alarm;
		}
		return ret;
	}
	
	public int onlyupdate(AlarmInfo alarm){
		ContentValues values = new ContentValues();
		values.put(AlarmInfo.ID, alarm.get_id());
		values.put(AlarmInfo.DESCRIPTION, alarm.description);
		values.put(AlarmInfo.TIME, alarm.timeinmillis);
		values.put(AlarmInfo.FREQUENCY, alarm.frequency);
		values.put(AlarmInfo.ENABLE, alarm.enable + "");
		values.put(AlarmInfo.DAYS_OF_WEEK, alarm.daysofweek);
		int ret = db.update(SQLiteHelper.DB_NAME, values, AlarmInfo.ID + "=" + alarm.get_id(), null);
		return ret;
	}
	
	public boolean delete(AlarmInfo alarm){
		int ret = db.delete(SQLiteHelper.DB_NAME, AlarmInfo.ID + "=" + alarm.get_id(), null);
		if(ret != 0)
			return true;
		else
			return false;
	}
	
	/**
	 * to clear all rows in database
	 */
	public int clearDatabase(){
		return db.delete(SQLiteHelper.DB_NAME, "1", null);
	}
	
//	public void rebuildTable(){
//		Constant.DB_VERSION++;
//		sqlHelper = new SQLiteHelper(context, DB_NAME, null, Constant.DB_VERSION);
//		db = sqlHelper.getWritableDatabase();
//	}

}
